<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

class MemberView_list extends AppView
{
	
	var $tmpl = 'list.html';
	
	
	function execute(&$obj, &$manager) {

		$this->addMsg('user_msg.ini');
	
		$tpl = &new tplTemplatez($this->template_dir . $this->tmpl);
		
		// filter 
		//$manager->setSqlParams($this->getFilterSql($manager));
		
		// header generate
		$bp =& $this->pageByPage($manager->limit, $manager->getRecordsSql());
		$tpl->tplAssign('header', $this->commonHeaderList($bp->nav));
		
		// sort generate
		$sort = &$this->getSort();
		$manager->setSqlParamsOrder($sort->getSql());
		
		// get records
		$users =& $this->stripVars($manager->getRecords($bp->limit, $bp->offset));
		
		// list records
		foreach($users as $row) {
			
			$obj->set($row);
			$obj->setFullName();
			
			$tpl->tplAssign('formated_date', $this->getFormatedDate($row['ts']));
			$tpl->tplAssign('name', $obj->getFullName());
			$tpl->tplAssign('email_name', $obj->getEmailName());
			$tpl->tplAssign('role', $row['role']);
			
			if($row['role_id'] == -1) {
				$tpl->tplAssign('role', $this->msg['all_roles_msg']);
			}
			
			
			$tpl->tplAssign($this->getViewListVars($obj->get('id'), $obj->get('active')));
			$tpl->tplParse(array_merge($obj->get(), $this->msg), 'row');
		}
		
		
		$tpl->tplAssign($this->msg);
		//$tpl->tplAssign($sort->toHtml());
		
		$tpl->tplParse();
		return $tpl->tplPrint(1);
	}
	
	
	function &getSort() {	
		//$sort = new TwoWaySort();
		$sort = new OneWaySort($_GET);
		$sort->setTitleMsg('asc',  $this->msg['sort_asc_msg']);
		$sort->setTitleMsg('desc', $this->msg['sort_desc_msg']);
		$sort->setSortItem('name_msg',     'last_name', 'last_name', $this->msg['name_msg'], 1);
		$sort->setSortItem('email_msg',    'email',     'email',     $this->msg['email_msg']);
		$sort->setSortItem('phone_msg',    'phone',     'phone',     $this->msg['phone_msg']);
		$sort->setSortItem('username_msg', 'username',  'username',  $this->msg['username_msg']);
		$sort->setSortItem('status_msg',   'active',    'active',    $this->msg['status_msg']);
		$sort->setSortItem('role_msg',     'role',      'role_id',   $this->msg['role_msg']);
		$sort->setSortItem('signing_date_msg','sign',    'date_registered',    $this->msg['signing_date_msg']);
		
		//$sort->getSql();
		//$sort->toHtml()
		return $sort;
	}
	
	
	function getFilter($manager) {

		$select = new FormSelect();
		$select->css_class = 'colorInput';
		$select->setSelectWidth(250);
		$select->setOnChangeSubmit(true);
		$select->setSelectName('filter');
		$select->setRange($manager->getRolesSelectRange(), 
		                  array('all'=>$this->msg['all_roles_msg'],
						        'none'=>$this->msg['none_role_msg']
								));
		
		$arr = $_GET; unset($arr[$select->select_name]);		

		
		$html = '<form action="" style="margin: 0px;">';
		$html .= http_build_hidden($arr);
		$html .= $select->select();
		$html .= '</form>';
		
		return $html;
	}
	
	
	function getFilterSql($manager) {
		
		// filter
		$arr = array();
		if(isset($_GET['filter']) && $_GET['filter'] != 'all') {
			
			$id = ($_GET['filter'] == 'none') ? '' : (int) $_GET['filter'];
			$arr[] = "AND role_id = '$id'";
		}
		
		return implode(" \n", $arr);
	}	
}
?>
